﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace ITSTOREBLL
{
    public class DonHangBLL
    {
        public Guid MaDonHang { get; set; }

        private ITStoreEntities db = new ITStoreEntities();

        public Guid TaoDonHang(HttpContextBase context)
        {
            if (context.Session["MaDonHang"] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    DonHang dh = new DonHang();
                    dh.dh_ID = Guid.NewGuid();
                    dh.dh_TaiKhoan = context.User.Identity.Name;
                    dh.dh_Gia = 0;
                    dh.dh_Ngay = DateTime.Now;
                    dh.dh_TrangThai = 1;
                    db.DonHangs.AddObject(dh);
                    db.SaveChanges();
                    MaDonHang = dh.dh_ID;
                }
                else
                {
                    //// Generate a new random GUID using System.Guid class
                    //Guid tempCartId = Guid.NewGuid();
                    //// Send tempCartId back to client as a cookie
                    //MaDonHang = tempCartId;
                    //DonHang dh = new DonHang();
                    //dh.dh_ID = tempCartId;
                    //dh.dh_TrangThai = 1;
                    //db.DonHangs.AddObject(dh);
                    //db.SaveChanges();
                    //MaDonHang = Guid.Parse(dh.dh_ID.ToString());
                    return Guid.Parse("00000000-0000-0000-0000-000000000000");
                }
                context.Session["MaDonHang"] = MaDonHang;
            }
            else
            {
                MaDonHang = (Guid)context.Session["MaDonHang"];
            }
            return MaDonHang;
        }

        public void ThemDonHang(int masanpham)
        {
            var qctdh = from c in db.ChiTietDonHangs
                       where c.ctdh_DonHang == MaDonHang && c.ctdh_SanPham == masanpham
                       select c;

            ChiTietDonHang ctdh = qctdh.FirstOrDefault();
            var sp = (from s in db.SanPhams
                      where s.sp_ID == masanpham
                      select s).FirstOrDefault();
            if (ctdh is ChiTietDonHang)
            {
                ctdh.ctdh_Gia = ctdh.ctdh_Gia + sp.sp_Gia;
                ctdh.ctdh_SoLuong++;
                ctdh.DonHang.dh_Gia = ctdh.DonHang.dh_Gia + sp.sp_Gia;
                ctdh.DonHang.dh_SoLuong++;
                db.SaveChanges();
            }
            else
            {
                DonHang dh = (from d in db.DonHangs
                             where d.dh_ID == MaDonHang
                             select d).FirstOrDefault();

                ChiTietDonHang actdh = new ChiTietDonHang();
                actdh.ctdh_SanPham = masanpham;
                actdh.ctdh_SoLuong = 1;
                actdh.ctdh_Gia = sp.sp_Gia;
                dh.ChiTietDonHangs.Add(actdh);
                dh.dh_Gia = dh.dh_Gia + sp.sp_Gia;
                dh.dh_SoLuong = dh.dh_SoLuong + 1;
                db.SaveChanges();
            }
        }
        public DonHang XemDanhSach()
        {
            var dh = from g in db.DonHangs.Include("ChiTietDonHang")
                     where g.dh_ID == MaDonHang
                     select g;
            return dh.FirstOrDefault();
        }

        public void LuuGioHang(HttpContextBase context)
        {
            var gh = (from g in db.DonHangs
                     where g.dh_ID == MaDonHang
                     select g).FirstOrDefault();
            gh.dh_TaiKhoan = context.User.Identity.Name;
            gh.dh_Gia = 100;
            gh.dh_Ngay = DateTime.Now;
            gh.dh_TrangThai = 2;
            db.SaveChanges();
        }

        public void ThanhToan(HttpContextBase context)
        {
            MaDonHang = (Guid)context.Session["MaDonHang"];
            DonHang dh = (from d in db.DonHangs
                          where d.dh_ID == MaDonHang
                          select d).FirstOrDefault();
            dh.dh_TrangThai = 3;
            db.SaveChanges();
        }

        public void XoaTatCaDonHang(HttpContextBase context)
        {
            MaDonHang = (Guid)context.Session["MaDonHang"];
            var ctdh = db.ChiTietDonHangs.Where(cart => cart.ctdh_DonHang == MaDonHang);
            foreach (var ctdhs in ctdh)
            {
                db.ChiTietDonHangs.DeleteObject(ctdhs);
            }
            var dh = db.DonHangs.Where(cart => cart.dh_ID == MaDonHang);
            foreach (var dhs in dh)
            {
                db.DonHangs.DeleteObject(dhs);
            }
            db.SaveChanges();
        }

        public void XoaMotSanPham(HttpContextBase context,int masanpham)
        {
            MaDonHang = (Guid)context.Session["MaDonHang"];
            var ctdh = db.ChiTietDonHangs.Where(cart => cart.ctdh_DonHang == MaDonHang && cart.ctdh_SanPham == masanpham);
            foreach (var ctdhs in ctdh)
            {
                db.ChiTietDonHangs.DeleteObject(ctdhs);
            }
            db.SaveChanges();
        }
    }
}
